package hz.mobileproject.pda.ui.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.blankj.utilcode.util.LogUtils;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lc on 2017/11/16.
 */

public abstract class BaseCustomAdapter<T, V> extends BaseAdapter {
    protected List<T> mList;
    protected Context mContext;
    protected LayoutInflater mInflater;

    public BaseCustomAdapter(Context mContext, List<T> list) {
        this.mContext = mContext;
        this.mInflater = LayoutInflater.from(mContext);
        this.mList = list;
    }

    public BaseCustomAdapter(Context mContext) {
        this.mContext = mContext;
        this.mInflater = LayoutInflater.from(mContext);
        this.mList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public T getItem(int position) {
        return mList == null ? null : mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        V vh;
        if (convertView == null) {
            convertView = mInflater.inflate(bindView(), parent, false);
            vh = createViewHolder();
            x.view().inject(vh, convertView);
            convertView.setTag(vh);
        } else {
            vh = (V) convertView.getTag();
        }
        bindData(position, convertView, vh);
        return convertView;
    }

    public List<T> getListData() {
        return mList;
    }

    public void clearAddAll(List<T> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.mList.clear();
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearAdd(List<T> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.mList.clear();
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearAddAllInvalidated(List<T> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.mList.clear();
        this.mList.addAll(list);
        notifyDataSetInvalidated();
    }

    public void addAll(List<T> list) {
        if (list == null) {
            return;
        }
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    public void addOne(T item) {
        if (item == null) {
            return;
        }
        this.mList.add(item);
        notifyDataSetChanged();
    }

    public void add(T item) {
        if (item == null) {
            return;
        }
        this.mList.add(item);
        notifyDataSetChanged();
    }

    public void add(int index, T item) {
        if (item == null) {
            return;
        }
        this.mList.add(index, item);
        notifyDataSetChanged();
    }

    public void deleteOne(int position) {
        if (position < 0 || position >= mList.size()) {
            LogUtils.e("position超出范围");
            return;
        }
        mList.remove(position);
        notifyDataSetChanged();
    }

    public boolean deleteOne(T t) {
        boolean remove = mList.remove(t);
        notifyDataSetChanged();
        return remove;
    }

    public void deleteAll() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    public void updateOne(int position, T t) {
        if (position < 0 || position >= mList.size()) {
            LogUtils.e("position超出范围");
            return;
        }
        mList.set(position, t);
        notifyDataSetChanged();
    }

    protected abstract int bindView();

    protected abstract V createViewHolder();

    protected abstract void bindData(int position, View convertView, V vh);

}
